package com.bigstar.lolen_e;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class JungleTimer extends Activity implements OnClickListener{
    @Override
    protected void onResume() {
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
    	super.onResume();
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
			AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
			int volume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
			audio.setStreamVolume(AudioManager.STREAM_MUSIC, ++volume, AudioManager.FLAG_SHOW_UI);	
			return true;
		}else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
			AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
			int volume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
			audio.setStreamVolume(AudioManager.STREAM_MUSIC, --volume, AudioManager.FLAG_SHOW_UI);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
        setContentView(R.layout.jungle_timer);
		int width = getResources().getDisplayMetrics().widthPixels;
		int height = getResources().getDisplayMetrics().heightPixels;
        float scaleX = (float) width / 480;
        float scaleY = (float) height / 800;
        
        
		
		
        
        AdView adview = (AdView)findViewById(R.id.adView);
        AdRequest re = new AdRequest(); 
        adview.loadAd(re);
        Button button = (Button)findViewById(R.id.left_blue);
        button.setOnClickListener(this);
		RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
				(int) (100*scaleX),
				(int) (50*scaleY));
		param.leftMargin = (int) (30*scaleX);
		param.topMargin = (int) (275*scaleY);
        button.setLayoutParams(param);
        button = (Button)findViewById(R.id.left_wolf);
        button.setOnClickListener(this);
        param = new RelativeLayout.LayoutParams(
				(int) (100*scaleX),
				(int) (50*scaleY));
        param.leftMargin = (int) (25*scaleX);
        param.topMargin = (int) (435*scaleY);
        button.setLayoutParams(param);
        button = (Button)findViewById(R.id.left_ghost);
        button.setOnClickListener(this);
        param = new RelativeLayout.LayoutParams(
				(int) (100*scaleX),
				(int) (50*scaleY));
        param.leftMargin = (int) (190*scaleX);
        param.topMargin = (int) (440*scaleY);
        button.setLayoutParams(param);
        button = (Button)findViewById(R.id.left_red);
        button.setOnClickListener(this);
        param = new RelativeLayout.LayoutParams(
				(int) (100*scaleX),
				(int) (50*scaleY));
        param.leftMargin = (int) (150*scaleX);
        param.topMargin = (int) (557*scaleY);
        button.setLayoutParams(param);
        button = (Button)findViewById(R.id.left_mini_golem);
        button.setOnClickListener(this);
        param = new RelativeLayout.LayoutParams(
				(int) (100*scaleX),
				(int) (50*scaleY));
        param.leftMargin = (int) (250*scaleX);
        param.topMargin = (int) (650*scaleY);
        button.setLayoutParams(param);
        button = (Button)findViewById(R.id.left_dragon);
        button.setOnClickListener(this);
        param = new RelativeLayout.LayoutParams(
				(int) (100*scaleX),
				(int) (50*scaleY));
        param.leftMargin = (int) (335*scaleX);
        param.topMargin = (int) (530*scaleY);
        button.setLayoutParams(param);
        button = (Button)findViewById(R.id.right_blue);
        button.setOnClickListener(this);
        param = new RelativeLayout.LayoutParams(
				(int) (100*scaleX),
				(int) (50*scaleY));
        param.leftMargin = (int) (375*scaleX);
        param.topMargin = (int) (440*scaleY);
        button.setLayoutParams(param);
        button = (Button)findViewById(R.id.right_wolf);
        button.setOnClickListener(this);
        param = new RelativeLayout.LayoutParams(
				(int) (100*scaleX),
				(int) (50*scaleY));
        param.leftMargin = (int) (265*scaleX);
		param.topMargin = (int) (345*scaleY);
        button.setLayoutParams(param);
        button = (Button)findViewById(R.id.right_ghost);
        button.setOnClickListener(this);
        param = new RelativeLayout.LayoutParams(
				(int) (100*scaleX),
				(int) (50*scaleY));
        param.leftMargin = (int) (300*scaleX);
		param.topMargin = (int) (240*scaleY);
        button.setLayoutParams(param);
        button = (Button)findViewById(R.id.right_red);
        button.setOnClickListener(this);
        param = new RelativeLayout.LayoutParams(
				(int) (100*scaleX),
				(int) (50*scaleY));
        param.leftMargin = (int) (290*scaleX);
        param.topMargin = (int) (150*scaleY);
        button.setLayoutParams(param);
        button = (Button)findViewById(R.id.right_mini_golem);
        button.setOnClickListener(this);
        param = new RelativeLayout.LayoutParams(
				(int) (100*scaleX),
				(int) (50*scaleY));
        param.leftMargin = (int) (160*scaleX);
		param.topMargin = (int) (45*scaleY);
        button.setLayoutParams(param);
        button = (Button)findViewById(R.id.right_baron);
        button.setOnClickListener(this);
        param = new RelativeLayout.LayoutParams(
				(int) (100*scaleX),
				(int) (50*scaleY));
        param.leftMargin = (int) (110*scaleX);
		param.topMargin = (int) (150*scaleY);
        button.setLayoutParams(param);
    }
	int time = 0;
	View view;
	CountDownTimer left_blue_timer;
	CountDownTimer left_wolf_timer;
	CountDownTimer left_ghost_timer;
	CountDownTimer left_red_timer;
	CountDownTimer left_mini_golem_timer;
	CountDownTimer left_dragon_timer;
	CountDownTimer right_blue_timer;
	CountDownTimer right_wolf_timer;
	CountDownTimer right_ghost_timer;
	CountDownTimer right_red_timer;
	CountDownTimer right_mini_golem_timer;
	CountDownTimer right_baron_timer;
	@Override
	public void onClick(View v) {
		view = v;
		switch(v.getId()){
		case R.id.left_blue: 
			time = 300;
			if(left_blue_timer == null){
				left_blue_timer = new CountDownTimer(time * 1000, 1000) {
					int spawn_time = time;
					Button button = (Button)view;
		            public void onTick(long millisUntilFinished) {   
		            	spawn_time--;
		            	button.setText(""+spawn_time);   
		            } 
		       
		            public void onFinish() {   
		            	if(spawn_time <= 2){
			            	MediaPlayer fileOfMusic = MediaPlayer.create(JungleTimer.this, R.raw.left_blue);
			                fileOfMusic.start();	
		            	}
		            	button.setText((String)button.getTag());   
						left_blue_timer.cancel();
						left_blue_timer = null;
		            }   
		        }.start(); 	
			}else{
				left_blue_timer.cancel();
				left_blue_timer.onFinish();
				left_blue_timer = null;
			}
	        break;
		case R.id.right_blue:
			time = 300;
			if(right_blue_timer == null){
				right_blue_timer = new CountDownTimer(time * 1000, 1000) {
					int spawn_time = time;
					Button button = (Button)view;
		            public void onTick(long millisUntilFinished) {   
		            	spawn_time--;
		            	button.setText(""+spawn_time);   
		            }   
		      
		            public void onFinish() {   
		            	if(spawn_time <= 2){
			            	MediaPlayer fileOfMusic = MediaPlayer.create(JungleTimer.this, R.raw.right_blue);
			                fileOfMusic.start();	
		            	}
		            	button.setText((String)button.getTag());   
						right_blue_timer.cancel();
						right_blue_timer = null;
		            }   
		        }.start(); 	
			}else{
				right_blue_timer.cancel();
				right_blue_timer.onFinish();
				right_blue_timer = null;
			}
	        break;
		case R.id.left_red:
			time = 300;
			if(left_red_timer == null){
				left_red_timer = new CountDownTimer(time * 1000, 1000) {
					int spawn_time = time;
					Button button = (Button)view;
		            public void onTick(long millisUntilFinished) {   
		            	spawn_time--;
		            	button.setText(""+spawn_time);   
		            }   
		      
		            public void onFinish() {   
		            	if(spawn_time <= 2){
			            	MediaPlayer fileOfMusic = MediaPlayer.create(JungleTimer.this, R.raw.left_red);
			                fileOfMusic.start();	
		            	}
		            	button.setText((String)button.getTag());   
						left_red_timer.cancel();
						left_red_timer = null;
		            }   
		        }.start(); 	
			}else{
				left_red_timer.cancel();
				left_red_timer.onFinish();
				left_red_timer = null;
			}
	        break;
		case R.id.right_red:
			time = 300;
			if(right_red_timer == null){
				right_red_timer = new CountDownTimer(time * 1000, 1000) {
					int spawn_time = time;
					Button button = (Button)view;
		            public void onTick(long millisUntilFinished) {   
		            	spawn_time--;
		            	button.setText(""+spawn_time);   
		            }   
		      
		            public void onFinish() {   
		            	if(spawn_time <= 2){
			            	MediaPlayer fileOfMusic = MediaPlayer.create(JungleTimer.this, R.raw.right_red);
			                fileOfMusic.start();	
		            	}
		            	button.setText((String)button.getTag()); 
						right_red_timer.cancel();
						right_red_timer = null;
		            }   
		        }.start(); 	
			}else{
				right_red_timer.cancel();
				right_red_timer.onFinish();
				right_red_timer = null;
			}
	        break;
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
		case R.id.left_ghost:
			time = 50;
			if(left_ghost_timer == null){
				left_ghost_timer = new CountDownTimer(time * 1000, 1000) {
					int spawn_time = time;
					Button button = (Button)view;
		            public void onTick(long millisUntilFinished) {   
		            	spawn_time--;
		            	button.setText(""+spawn_time);   
		            }   
		      
		            public void onFinish() {   
		            	if(spawn_time <= 2){
			            	MediaPlayer fileOfMusic = MediaPlayer.create(JungleTimer.this, R.raw.left_ghost);
			                fileOfMusic.start();	
		            	}
		            	button.setText((String)button.getTag());   
						left_ghost_timer.cancel();
						left_ghost_timer = null;
		            }   
		        }.start(); 	
			}else{
				left_ghost_timer.cancel();
				left_ghost_timer.onFinish();
				left_ghost_timer = null;
			}
	        break;
		case R.id.right_ghost:
			time = 50;
			if(right_ghost_timer == null){
				right_ghost_timer = new CountDownTimer(time * 1000, 1000) {
					int spawn_time = time;
					Button button = (Button)view;
		            public void onTick(long millisUntilFinished) {   
		            	spawn_time--;
		            	button.setText(""+spawn_time);   
		            }   
		      
		            public void onFinish() {   
		               	if(spawn_time <= 2){
			            	MediaPlayer fileOfMusic = MediaPlayer.create(JungleTimer.this, R.raw.right_ghost);
			                fileOfMusic.start();	
		               	}
		            	button.setText((String)button.getTag());   
						right_ghost_timer.cancel();
						right_ghost_timer = null;
		            }   
		        }.start(); 	
			}else{
				right_ghost_timer.cancel();
				right_ghost_timer.onFinish();
				right_ghost_timer = null;
			}
	        break;
		case R.id.left_wolf:
			time = 60;
			if(left_wolf_timer == null){
				left_wolf_timer = new CountDownTimer(time * 1000, 1000) {
					int spawn_time = time;
					Button button = (Button)view;
		            public void onTick(long millisUntilFinished) {   
		            	spawn_time--;
		            	button.setText(""+spawn_time);   
		            }   
		      
		            public void onFinish() {   
		               	if(spawn_time <= 2){
			            	MediaPlayer fileOfMusic = MediaPlayer.create(JungleTimer.this, R.raw.left_wolf);
			                fileOfMusic.start();	
		               	}
		            	button.setText((String)button.getTag());
						left_wolf_timer.cancel();
						left_wolf_timer = null;
		            }   
		        }.start(); 	
			}else{
				left_wolf_timer.cancel();
				left_wolf_timer.onFinish();
				left_wolf_timer = null;
			}
	        break;
		case R.id.right_wolf:
			time = 60;
			if(right_wolf_timer == null){
				right_wolf_timer = new CountDownTimer(time * 1000, 1000) {
					int spawn_time = time;
					Button button = (Button)view;
		            public void onTick(long millisUntilFinished) {   
		            	spawn_time--;
		            	button.setText(""+spawn_time);   
		            }   
		      
		            public void onFinish() {   
		              	if(spawn_time <= 2){
			            	MediaPlayer fileOfMusic = MediaPlayer.create(JungleTimer.this, R.raw.right_wolf);
			                fileOfMusic.start();	
		              	}
		            	button.setText((String)button.getTag());   
						right_wolf_timer.cancel();
						right_wolf_timer = null;
		            }   
		        }.start(); 	
			}else{
				right_wolf_timer.cancel();
				right_wolf_timer.onFinish();
				right_wolf_timer = null;
			}
	        break;
		case R.id.left_mini_golem:
			time = 60;
			if(left_mini_golem_timer == null){
				left_mini_golem_timer = new CountDownTimer(time * 1000, 1000) {
					int spawn_time = time;
					Button button = (Button)view;
		            public void onTick(long millisUntilFinished) {   
		            	spawn_time--;
		            	button.setText(""+spawn_time);   
		            }   
		      
		            public void onFinish() {   
		             	if(spawn_time <= 2){
		                   	MediaPlayer fileOfMusic = MediaPlayer.create(JungleTimer.this, R.raw.left_mini_golem);
			                fileOfMusic.start();            		
		             	}
		            	button.setText((String)button.getTag());   
						left_mini_golem_timer.cancel();
						left_mini_golem_timer = null;
		            }   
		        }.start(); 	
			}else{
				left_mini_golem_timer.cancel();
				left_mini_golem_timer.onFinish();
				left_mini_golem_timer = null;
			}
	        break;
		case R.id.right_mini_golem:
			time = 60;
			if(right_mini_golem_timer == null){
				right_mini_golem_timer = new CountDownTimer(time * 1000, 1000) {
					int spawn_time = time;
					Button button = (Button)view;
		            public void onTick(long millisUntilFinished) {   
		            	spawn_time--;
		            	button.setText(""+spawn_time);   
		            }   
		      
		            public void onFinish() {   
		              	if(spawn_time <= 2){
			            	MediaPlayer fileOfMusic = MediaPlayer.create(JungleTimer.this, R.raw.right_mini_golem);
			                fileOfMusic.start();	
		              	}
		            	button.setText((String)button.getTag());   
						right_mini_golem_timer.cancel();
						right_mini_golem_timer = null;
		            }   
		        }.start(); 	
			}else{
				right_mini_golem_timer.cancel();
				right_mini_golem_timer.onFinish();
				right_mini_golem_timer = null;
			}
	        break;
			
			
			
			
			
		case R.id.right_baron:
			time = 420;
			if(right_baron_timer == null){
				right_baron_timer = new CountDownTimer(time * 1000, 1000) {
					int spawn_time = time;
					Button button = (Button)view;
		            public void onTick(long millisUntilFinished) {   
		            	spawn_time--;
		            	button.setText(""+spawn_time);   
		            }   
		      
		            public void onFinish() {   
		               	if(spawn_time <= 2){
			            	MediaPlayer fileOfMusic = MediaPlayer.create(JungleTimer.this, R.raw.right_baron);
			                fileOfMusic.start();	
		               	}
		            	button.setText((String)button.getTag());   
		    			right_baron_timer.cancel();
						right_baron_timer = null;
		            }   
		        }.start(); 	
			}else{
				right_baron_timer.cancel();
				right_baron_timer.onFinish();
				right_baron_timer = null;
			}
	        break;
		case R.id.left_dragon:
			time = 360;
			if(left_dragon_timer == null){
				left_dragon_timer = new CountDownTimer(time * 1000, 1000) {
					int spawn_time = time;
					Button button = (Button)view;
		            public void onTick(long millisUntilFinished) {   
		            	spawn_time--;
		            	button.setText(""+spawn_time);   
		            }   
		      
		            public void onFinish() {
		            	if(spawn_time <= 2){
			            	MediaPlayer fileOfMusic = MediaPlayer.create(JungleTimer.this, R.raw.left_dragon);
			                fileOfMusic.start();	
		            	}
		            	button.setText((String)button.getTag());   
						left_dragon_timer.cancel();
						left_dragon_timer = null;
		            }   
		        }.start(); 	
			}else{
				left_dragon_timer.cancel();
				left_dragon_timer.onFinish();
				left_dragon_timer = null;
			}
	        break;
		}
	}
	
    
    @Override
    protected void onStop() {
    	super.onStop();
    	if(left_blue_timer != null){
        	left_blue_timer.cancel();	
    	}
    	if(left_wolf_timer != null){
    		left_wolf_timer.cancel();	
    	}
     	if(left_ghost_timer != null){
     		left_ghost_timer.cancel();	
    	}
     	if(left_red_timer != null){
     		left_red_timer.cancel();	
    	}
     	if(left_mini_golem_timer != null){
     		left_mini_golem_timer.cancel();	
    	}
     	if(left_dragon_timer != null){
     		left_dragon_timer.cancel();	
    	}
     	if(right_blue_timer != null){
     		right_blue_timer.cancel();	
    	}
     	if(right_wolf_timer != null){
     		right_wolf_timer.cancel();	
    	}
     	if(right_ghost_timer != null){
     		right_ghost_timer.cancel();	
    	}
     	if(right_red_timer != null){
     		right_red_timer.cancel();	
    	}
     	if(right_mini_golem_timer != null){
     		right_mini_golem_timer.cancel();	
    	}
     	if(right_baron_timer != null){
     		right_baron_timer.cancel();	
    	}
    }
}
